package com.nassir.chatroom.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nassir.chatroom.model.ClientModel;

/**
 * 客户端存储
 * @author nassir.wen
 * @date 2012-12-5下午4:05:50
 * @Company: MSD. 
 * @Copyright: Copyright (c) 2012
 */
public class ClientManager {

	private static class ClientManagerHolder {
		static final ClientManager uniqueInstance = new ClientManager();
	}

	private ClientManager() {
	}

	public static ClientManager getInstance() {
		return ClientManagerHolder.uniqueInstance;
	}
	
	/**
	 * 客户端
	 */
	private List<ClientModel> clients = new ArrayList<ClientModel>(50); 
	private Map<Integer,ClientModel> clientCache = new HashMap<Integer,ClientModel>(50);
	
	public List<ClientModel> getClients(){
		return clients;
	}
	
	/**
	 * 删除客户端
	 * @author nassir.wen
	 * @date 2012-12-5下午3:50:53
	 * @param clientId 客户端ID
	 * @return
	 */
	public boolean remove(Integer clientId){
		clientCache.remove(clientId);
		clients.remove(new ClientModel(clientId));
		return true;
	}
	
	/**
	 * 新增客户端
	 * @author nassir.wen
	 * @date 2012-12-5下午3:50:45
	 * @param e
	 */
	public void add(ClientModel e){
		clients.add(e);
		clientCache.put(e.getClientId(), e);
	}
	
	/**
	 * 获取客户端
	 * @author nassir.wen
	 * @date 2012-12-5下午4:18:10
	 * @param clientId
	 * @return
	 */
	public ClientModel getClient(Integer clientId){
		return clientCache.get(clientId);
	}
	
	
}
